var addressId = '';
var ifCart = GetQueryString('ifCart');
var goodsId = '';
var goodsNum = '';
var speId = '';
var goodsImg = '';
var goodsName = '';
var speName = '';
var shopName = '';
var totalMoney = '';
var unitMoney = '';
$(function () {
    if(ifCart){
        doCart();
    }else{
        var orderUrl = decodeURI(decodeURI(window.location.href)).split('=');
        goodsId = orderUrl[1].split('&')[0];
        goodsNum = orderUrl[2].split('&')[0];
        speId = orderUrl[3].split('&')[0];
        if(speId==undefined||speId==""||speId==null){
            speId = '';
        }
        goodsImg = orderUrl[4].split('&')[0];
        goodsName = orderUrl[5].split('&')[0];
        speName = orderUrl[6].split('&')[0];
        shopName = orderUrl[7].split('&')[0];
        totalMoney = orderUrl[8].split('&')[0];
        unitMoney = orderUrl[9];
        doGoods();
    }
    $.ajax({
        url:'/user/getDefault.json',
        dataType:'json',
        data:{
            token:getItem('token'),
        },
        type:'post',
        success:function (data) {
            if(data.code==0){
                if(data.msg=='您还没有添加收货地址，请添加收货地址'){
                    mui.confirm( '您还没有收获地址，赶快去先设置一个吧',' ', ['取消', '确认'], function(e) {
                        if (e.index == 1) {
                            window.location.href="add_address2.html";
                        } else {
                            window.location.href="personal_v2.html";
                        }
                    })
                }
                return;
            }
            if(data.code==1){
                addressId = data.datas.id;
                $('.name').html(data.datas.name);
                $('.phone').html(data.datas.phone);
                $('.address_detail').html(data.datas.province+data.datas.city+data.datas.county+data.datas.detail);
            }
        }
    })
})

function doGoods() {
    $('<div class="goods">\n' +
        '<div class="shopName">'+shopName+'</div>\n' +
        '<div class="detailBox mui-clearfix">\n' +
        '<div class="goods-img-box">\n' +
        '<img src="'+goodsImg+'">\n' +
        '</div>\n' +
        '<div class="goods-product-box">\n' +
        '<p class="goodsName">'+goodsName+'</p>\n' +
        '<span class="guige">'+speName+'</span>\n' +
        '<span class="price">'+unitMoney+'<i>×'+goodsNum+'</i></span>\n' +
        '</div></div></div><div class="total">\n' +
        '<span class="total_price">小计： <i>¥ '+totalMoney+'</i></span>\n' +
        '<span class="total_count">共'+goodsNum+'件商品</span>\n' +
        '</div><div class="line"></div>').appendTo($('.goodsBox'));
    $('.sum_price').html('合计：<i>¥ '+totalMoney+'</i>')
}

function doCart() {
    $.ajax({
        url: '/ShoppingCartC/getUserShoppingCart.json',
        dataType: 'json',
        data: {
            token: getItem('token'),
            isSettlement:false,
        },
        type: 'post',
        success: function (data) {
            if (data.code == 1) {
                var cartData = data.datas;
                if (cartData.length > 0) {
                    for (var i = 0; i < cartData.length; i++) {
                        $('<div class="goods"><div class="shopName">' + cartData[i].shopName + '</div>'+
                            '<div class="detailBox mui-clearfix" id="OrderBox' + cartData[i].shopId + '">'+
                            '</div></div><div class="total">'+
                            '<span class="total_price">小计： <i>¥ '+cartData[i].shopSelectMoney+'</i></span>'+
                            '<span class="total_count">共'+cartData[i].shopSelectCount+'件商品</span></div><div class="line"></div>').appendTo($('.goodsBox'));
                        for (var j = 0; j < cartData[i].goods.length; j++) {
                            var payOnDelivery = '';
                            if (cartData[i].goods[j].payOnDelivery == 0) { //不支持货到付款
                                payOnDelivery = ''
                            } else {
                                payOnDelivery = '<span class="payCash">货到付款</span>'; //支持货到付款
                            }
                            var goodsMainImg = cartData[i].goods[j].goodsMainImage.split(',')[0];
                            $('<div class="mui-clearfix"><div class="goods-img-box"><img src="'+goodsMainImg+'"/></div>'+
                                '<div class="goods-product-box" goodsId="'+cartData[i].goods[j].goodsId+'" goodsNum="'+cartData[i].goods[j].count+'" speId="'+cartData[i].goods[j].specificationId+'">'+
                                '<p class="goodsName">' + payOnDelivery + cartData[i].goods[j].goodsName + '</p>'+
                                '<span class="guige">' + cartData[i].goods[j].propertiesName + '</span>'+
                                '<span class="price">￥' + cartData[i].goods[j].memberPrice + '<i>×' + cartData[i].goods[j].count + '</i></span></div></div>').appendTo($('#OrderBox' + cartData[i].shopId + ''))
                        }
                    }
                    $('.sum_price').html('合计：<i>¥ '+data.totalMoney+'</i>')
                }
            }
        }
    })
}

//提交订单
$('.submit').click(function () {
    var carStr = '';
    $('.goods-product-box').each(function () {
        carStr+=$(this).attr('goodsId')+':'+$(this).attr('goodsNum')+':'+$(this).attr('speId')+',';
    })
    console.log(speId);
    $.ajax({
        url:'/zuoDanC/submitOrder.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            addressId:addressId,
            //carStr:carStr,
            goodsId:goodsId,
            goodsSpecificationId:speId,
            num:goodsNum,
        },
        type:'post',
        success:function (data) {
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }if(data.code==1){
                //mui.alert('提交订单成功',function () {
                    window.location.href="userOrder_v2.html?zuoDanStateValue=4"
                //})
            }
        }
    })
})

//跳转至地址
$('.address_detail').click(function () {
    window.location.href="../html/manage_address.html"
})